package com.lx.entity;

import com.lx.annotation.Note;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;

@Note("用来返回两个字段")
public final class Pair<L, R> implements Map.Entry<L, R>, Serializable {
    private static final long serialVersionUID = 491L;
    public static final Pair EMPTY_ARRAY = new Pair<>(null,null);
    public final L left;
    public final R right;
    public Pair(L left, R right) {
        this.left = left;
        this.right = right;
    }

    public static <L, R> Pair<L, R> of(Map.Entry<L, R> pair) {
        L left;
        R right;
        if (pair != null) {
            left = (L)pair.getKey();
            right = (R)pair.getValue();
        } else {
            left = null;
            right = null;
        }

        return new Pair<L, R>(left, right);
    }

    public final L getKey() {
        return (L)this.getLeft();
    }

    public L getLeft() {
        return this.left;
    }

    public R getRight() {
        return this.right;
    }

    public R getValue() {
        return (R)this.getRight();
    }

    public R setValue(R value) {
        throw new UnsupportedOperationException();
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        } else if (!(obj instanceof Map.Entry)) {
            return false;
        } else {
            Map.Entry<?, ?> other = (Map.Entry)obj;
            return Objects.equals(this.getKey(), other.getKey()) && Objects.equals(this.getValue(), other.getValue());
        }
    }

    public int hashCode() {
        return Objects.hashCode(this.getKey()) ^ Objects.hashCode(this.getValue());
    }

    public String toString() {
        return "(" + this.getLeft() + ',' + this.getRight() + ')';
    }

    public String toString(String format) {
        return String.format(format, this.getLeft(), this.getRight());
    }
}
